package com.iflytek.universitymanagement.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iflytek.universitymanagement.config.Result;
import com.iflytek.universitymanagement.domain.Student;
import com.iflytek.universitymanagement.domain.Teacher;
import com.iflytek.universitymanagement.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author louyp
 * @since 2024-11-26
 */

@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    TeacherService teacherService;

    @GetMapping("/selectById/{teacherId}")
    public Result selectByTeacherId(@PathVariable Integer teacherId) {
        Teacher teacher = teacherService.getById(teacherId);
        return teacher != null ? Result.success(teacher) : Result.fail("教师信息未找到");
    }

        /**
         * 修改
         */
    @PutMapping("/update")
    public Result update(@RequestBody Teacher teacher) {
        boolean success = teacherService.updateById(teacher);
        return success ? Result.success("保存成功") : Result.error("保存失败");
    }



    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Teacher teacher) {
        teacherService.add(teacher);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        teacherService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        teacherService.removeByIds(ids);
        return Result.success();
    }


    /**
     * 分页查询
     */
//    @GetMapping("/selectPage")
//    public Result selectPage(Teacher teacher,
//                             @RequestParam(defaultValue = "1") Integer pageNum,
//                             @RequestParam(defaultValue = "10") Integer pageSize) {
//        IPage<Teacher> page = teacherService.selectPage(teacher, pageNum, pageSize);
//        return Result.success(page);
//    }

    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(required = false) String name,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<Teacher>().orderByAsc("teacher_id");
        if (name != null && !name.trim().isEmpty()) {
            queryWrapper.like("name", name);
        }
        Page<Teacher> page = teacherService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

}



